/**
 * Copyright 2022 Huawei Technologies Co., Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef MINDSPORE_LITE_TOOLS_CONVERTER_MICRO_CODER_OPCODERS_NNACL_INT8_PAD_INT8_CODER_H_
#define MINDSPORE_LITE_TOOLS_CONVERTER_MICRO_CODER_OPCODERS_NNACL_INT8_PAD_INT8_CODER_H_

#include <string>
#include <memory>
#include <vector>
#include "coder/opcoders/op_coder.h"
#include "nnacl/int8/quantize.h"
#include "nnacl/int8/pad_int8.h"
#include "nnacl/pad_parameter.h"

namespace mindspore::lite::micro::nnacl {
class PadInt8Coder final : public OperatorCoder {
 public:
  PadInt8Coder(const std::vector<Tensor *> &in_tensors, const std::vector<Tensor *> &out_tensors,
               const LiteGraph::Node *node, size_t node_index, Target target)
      : OperatorCoder(in_tensors, out_tensors, node, node_index, target) {}

  ~PadInt8Coder() override {
    if (pad_quant_arg_.in_quant_args_ != nullptr) {
      free(pad_quant_arg_.in_quant_args_);
      pad_quant_arg_.in_quant_args_ = nullptr;
    }
    if (pad_quant_arg_.out_quanr_args_ != nullptr) {
      free(pad_quant_arg_.out_quanr_args_);
      pad_quant_arg_.out_quanr_args_ = nullptr;
    }
    if (pad_quant_arg_.constant_value_ != nullptr) {
      free(pad_quant_arg_.constant_value_);
      pad_quant_arg_.constant_value_ = nullptr;
    }
  }

  int Prepare(CoderContext *context) override;

  int DoCode(CoderContext *const context) override;

 private:
  int InitPadParam();
  int ExtendPaddings(int *paddings, int length, const int *ori_paddings, int ori_length) const;
  int CopyPaddingFromInput();
  int CalculateStrides();
  int HandleMirrorPad();
  int CheckPaddings(const int *paddings, int length, const int *input_shape, int mode);
  PadParameter *pad_param_ = nullptr;
  int in_dims_[COMM_SHAPE_SIZE] = {0};
  int out_dims_[COMM_SHAPE_SIZE] = {0};

  int in_strides[DEFAULT_PAD_NDIMS];
  int out_strides[DEFAULT_PAD_NDIMS];
  int mirror_offset_;
  PadQuantArg pad_quant_arg_;
};
}  // namespace mindspore::lite::micro::nnacl
#endif  // MINDSPORE_LITE_TOOLS_CONVERTER_MICRO_CODER_OPCODERS_NNACL_INT8_PAD_INT8_CODER_H_
